/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.flexengine.utils;

import com.flexengine.logger.Logger;
import java.awt.Font;
import java.awt.FontFormatException;
import java.awt.GraphicsEnvironment;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;

/**
 * @author Luann R. Athayde
 * @version 1.0
 * @since 1.0
 */
public class FontManager {

    private FontManager()   {
    }

    public static final String[] defaultFonts = {
        "FreeMono","FreeMonoBold",
        "FreeSans","FreeSansBold"};
    
    public static final String   defaultFont;
    /*
     * Available fonts for game...
     */
    private static String[] fonts;
    static {
        defaultFont = Font.SERIF;
        fonts = new String[] {};
    }
    
    public static void loadAllAvailableFonts() {
        try {
            fonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
            File fontFolder = new File(ResourceManager.getLocale(ResourceManager.FONTS_PATH));
            File aFonts[] = fontFolder.listFiles(new FilenameFilter() {
                @Override
                public boolean accept(File dir, String name) {
                    return name.endsWith(".ttf");
                }
            });
            for(File font : aFonts)   {
                boolean install = installFont(font.getPath());
                if( install ) {
                    Logger.logMessage(
                        FontManager.class.getSimpleName()+
                        " - loadAllAvailableFonts(): Font ["+font+"] installed sussefully...");
                }
            }
            fonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
        } catch(Exception e) {
            Logger.logError("FontManager::loadAllAvailableFonts(): "+e);
        }
    }

    public static boolean containsFont(String fonte)    {
        for(String f : fonts) {
            if( f.equals(fonte) ) {
                return true;
            }
        }
        return false;
    }
    public static Font getFont(String fonte)    {
        return new Font(fonte,Font.PLAIN,12);
    }
    public static Font getFont(String fonte, int style)    {
        return new Font(fonte,style,12);
    }
    public static Font getFont(String fonte, int style, int size)    {
        return new Font(fonte,style,size);
    }
    
    // -- Installing methods for fonts...
    public static boolean installFont(String file) {
        try {
            if( file != null ) {
                if( file.endsWith(".ttf") ) {
                    String tmp[] = file.split("/");
                    String containsFont = tmp[tmp.length-1].replace(".ttf", "");
                    //System.out.println("containsFont = "+containsFont);
                    if( !containsFont(containsFont) ) {
                        Font fnt = Font.createFont(
                            Font.TRUETYPE_FONT,
                            new File(file));
                        return GraphicsEnvironment.getLocalGraphicsEnvironment().registerFont(fnt);
                    } else {
                        Logger.logWarning(FontManager.class.getSimpleName()+" - installFont("+file+"): This font is allread available on system...");
                    }
                }
                else {
                    Logger.logWarning(FontManager.class.getSimpleName()+" - installFont("+file+"): Unsuportted font file...");
                }
            } else {
                Logger.logWarning(FontManager.class.getSimpleName()+" - installFont("+file+"): File to load the font is null...");
            }
        } catch(FontFormatException | IOException e) {
            Logger.logError(FontManager.class.getSimpleName()+" - installFont("+file+"): "+e);
        }
        return false;
    }

}